﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

namespace Royaumes_Libres.Database
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>Francis</remarks>
    public class cWorld
    {
        protected int pPkWorld;
        protected string pName;
        protected string pDescFr;
        protected string pDescEn;

        public cWorld(int pkworld, string name)
        {
            this.pPkWorld = pkworld;
            this.pName = name;
        }

        public cWorld(int pkworld, string name, string descfr, string descen)
        {
            this.pPkWorld = pkworld;
            this.pName = name; 
            this.pDescFr = descfr;
            this.pDescEn = descen;
        }

        public int PkWorld { get { return this.pPkWorld; } }
        public string Name { get { return this.pName; } }
        public string DescFr { get { return this.pDescFr; } }
        public string DescEn { get { return this.pDescEn; } }

        /// <summary>
        /// Retourne un array de tout les mondes ou le user a un royaume
        /// </summary>
        /// <param name="PkUser"></param>
        /// <returns></returns>
        public static cWorld[] GetUserWorld(int PkUser)
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();
            List<cWorld> lstWorld = new List<cWorld>();

            try
            {
                cmd.Connection.Open();

                cmd.CommandText = "cde.spGetUserWorld";
                cmd.Parameters.Add(cSQL.Param("@PkUser", PkUser, SqlDbType.Int));

                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    lstWorld.Add(new cWorld(dr.GetInt32(0), dr.GetString(1), dr.GetString(2), dr.GetString(3)));
                }

                cmd.Connection.Close();

                return lstWorld.ToArray();
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cWorld(): " + ex.Message);
            }

        }

        /// <summary>
        /// Retourne tout les mondes dans un array
        /// </summary>
        /// <returns></returns>
        public static cWorld[] GetAllWorlds()
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();
            List<cWorld> lstWorld = new List<cWorld>();

            try
            {
                cmd.Connection.Open();

                cmd.CommandText = "cde.spGetAllWorlds";

                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    lstWorld.Add(new cWorld(dr.GetInt32(0), dr.GetString(1), dr.GetString(2), dr.GetString(3)));
                }

                cmd.Connection.Close();

                return lstWorld.ToArray();
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cWorld(): " + ex.Message);
            }
        }
        /// <summary>
        /// Retourne le controle ucWorld
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public ucWorlds GetControl(Page p)
        {
            ucWorlds uc = (ucWorlds)p.LoadControl("ucWorlds.ascx");

            return uc;
        }

    }
}